﻿using C556Backside.Commands.Manager;
using C556Backside.Services;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using C556Backside.ReturnModels;
using C556Backside.Models;
using static C556Backside.ReturnModels.LoginReturn<C556Backside.Models.ManagerModel>;
using C556Backside.ViewModel.Manager;
using Microsoft.EntityFrameworkCore;
using C556Backside.ViewModel.Organization;

namespace C556Backside.Controllers;
[ApiController]
[Route("SelectIdentity/Manager")]
[EnableCors("CorsPolicy")]
public class ManagerController
{
    public C556DBContext DBContext { get; set; }
    public ManagerController(C556DBContext c556DBContext)
    {
        DBContext = c556DBContext;
    }
    [HttpPost]
    [Route("Login")]
    public ReturnModel Login([FromBody] CManagerLogin login)
    {
        if (login == null)
        {
            return new ErrorReturn { ErrorMessage = "未提交数据" };
        }
        try
        {
            var result = DBContext.Managers.SingleOrDefault(o => o.Name == login.UserName && o.Password == login.Password);
            if (result != null)
            {
                return new LoginReturn<ManagerModel>(result) { Identity = LoginIdentity.Manager };
            }
            return new ErrorReturn { ErrorMessage = "未找到这位管理员，请确认账号和密码" };
        }
        catch
        {
            return new ErrorReturn { ErrorMessage = "出现异常，请联系系统管理人员" };
        }
    }

    [HttpGet]
    [Route("GetAllVoluntary")]
    public List<VGetAllVoluntary> GetAllVoluntary()
    {
        return DBContext.Voluntary.Select(o => new VGetAllVoluntary
        {
            VoluntaryName = o.VoluntaryName,
            VoluntaryTelephone = o.VoluntaryTelephone,
            Province = o.Province,
            City = o.City,
            Area = o.Area
        }).ToList();
    }

    [HttpGet]
    [Route("GetAllOrganization")]
    public List<VGetAllOrganization> GetAllOrganization()
    {
        return DBContext.Organizations.Select(o => new VGetAllOrganization
        {
            OrganizationName = o.OrganizationName,
            OrganizationTelephone = o.OrganizationTelephone,
            Province = o.Province,
            City = o.City,
            Area = o.Area
        }).ToList();
    }

    [HttpGet]
    [Route("GetAllActivity")]
    public List<VGetAllActivity> GetAllActivity()
    {
        return DBContext.VoluntaryActivities.Include(o=>o.OrganizationModel).Include(o=>o.ActivityTypeModel).Select(o => new VGetAllActivity
        {
            ActivityID= o.ActivityID,
            ActivityName = o.ActivityName,
            ActivityCreateTime = o.ActivityCreateTime,
            ActivityComplete = o.ActivityComplete,
            ActivityCompleteTime = o.ActivityCompleteTime,
            ActivityTypeModel = o.ActivityTypeModel,
            IsDelete = o.IsDelete,
            Province = o.Province,
            City = o.City,
            Area = o.Area,
            DetailedAddress = o.DetailedAddress,
            OrganizationModel = o.OrganizationModel
        }).ToList();
    }

    [HttpGet]
    [Route("GetVoluntaryByActivity")]
    public List<VGetVoluntaryByActivity> GetVoluntaryByActivity([FromQuery] CGetVoluntaryByActivity model)
    {
        var va = DBContext.VAVRelationshipsTable.Where(o => o.ActivityID == model.ActivityID);
        var vID = va.Select(o => o.VoluntaryID).ToArray();
        var v = DBContext.Voluntary.Where(o => vID.Contains(o.VoluntaryID));
        return v.Select(o => new VGetVoluntaryByActivity { VoluntaryName = o.VoluntaryName, VoluntaryTelephone = o.VoluntaryTelephone, Province = o.Province, City = o.City, Area = o.Area }).ToList();
    }
}
